import pandas as pd
import matplotlib.pyplot as plt

# 读取数据
data = pd.read_csv('order_train1_new.csv', parse_dates=['order_date'])

# 根据订单日期将数据进行排序
data = data.sort_values(by='order_date')

# 按照月初、月中、月末将订单需求量进行分组
time_bins = [0, 10, 20, 31]
time_labels = ['Beginning', 'Middle', 'End']
data['order_date_category'] = pd.cut(data['order_date'].dt.day, bins=time_bins, labels=time_labels)

# 统计不同时间段的订单需求量
demand_by_time = data.groupby('order_date_category')['ord_qty'].sum()

# 绘制不同时间段的订单需求量柱状图，并设置图的大小
plt.figure(figsize=(8, 6))
demand_by_time.plot(kind='bar')
plt.title('Distribution of Order Demand by Time')
plt.xlabel('Time')
plt.ylabel('Order Demand')
plt.show()

# 计算每个月的平均订单需求量和总订单需求量
month_demand = data.groupby(pd.Grouper(key='order_date', freq='M'))['ord_qty'].agg(['mean', 'sum'])
month_demand.index = month_demand.index.strftime('%y-%m')

# 绘制月平均需求量折线图，并设置图的大小
fig, ax = plt.subplots(figsize=(8, 6))
ax.plot(month_demand.index, month_demand['mean'])
ax.set_title('Distribution of Average Order Demand by Month')
ax.set_xlabel('Month')
ax.set_ylabel('Average Order Demand')
plt.xticks(rotation=55) # 旋转x轴标签
plt.show()

# 绘制各月总需求折线图，并设置图的大小
fig, ax = plt.subplots(figsize=(30, 6))
ax.plot(month_demand.index, month_demand['sum'])
ax.set_title('Distribution of Total Order Demand by Month')
ax.set_xlabel('Month')
ax.set_ylabel('Total Order Demand')
plt.show()
